package com.rjpacket.supernews.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;
import com.rjpacket.supernews.app.utils.SmileParser;

import java.util.LinkedList;
import java.util.List;


public class CommentActivity extends ActionBarActivity implements View.OnClickListener {

    private EditText editComment;
    private RelativeLayout layout;

    private EditText inputComment;

    private Button btnFabiao;
    private ImageView qweibo;
    private ImageView sina;
    private ImageView emoji;
    private RelativeLayout relativeLayout;
    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        relativeLayout = (RelativeLayout) findViewById(R.id.show_emoji);
        gridView = (GridView) relativeLayout.findViewById(R.id.emoji_gridview);

        //点击弹出编辑框
        editComment = (EditText) findViewById(R.id.activity_duanzi_detail_edit_comment);
        editComment.setOnClickListener(this);

        inputComment = (EditText) findViewById(R.id.activity_duanzi_detail_input);
        inputComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String comment = inputComment.getText().toString();
                //调用工具类,当输入表情对应的文字时,用表情替换
                SmileParser parser = new SmileParser(CommentActivity.this);
                inputComment.setText(parser.replace(comment));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //输入内容的布局
        layout = (RelativeLayout) findViewById(R.id.activity_duanzi_detail_show_edittext);

        int visibility = layout.getVisibility();
        //如果当前显示输入内容的布局,则找到里面的各种控件
        if (View.VISIBLE == visibility) {

            btnFabiao = (Button) findViewById(R.id.btn_activity_duanzi_fabiao);
            qweibo = (ImageView) findViewById(R.id.activity_duanzi_detail_img_qweibo);
            sina = (ImageView) findViewById(R.id.activity_duanzi_detail_img_sina);
            emoji = (ImageView) findViewById(R.id.activity_duanzi_detail_img_emoji);

            btnFabiao.setOnClickListener(this);
            qweibo.setOnClickListener(this);
            sina.setOnClickListener(this);
            emoji.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {

        int id = v.getId();
        switch (id) {
            case R.id.activity_duanzi_detail_edit_comment:
                layout.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_activity_duanzi_fabiao:
                //TODO 检测当前网络状态,如果没网,Toast
                //TODO 检测当前是否登录,如果没有,跳转到登录界面 LoginActivity
                //发表成功之后,将评论界面的可见性设为不可见
                layout.setVisibility(View.GONE);
                break;
            case R.id.activity_duanzi_detail_img_emoji:
                //TODO 弹出emoji图片清单
                relativeLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.activity_duanzi_detail_img_qweibo:
                //TODO 分享到QQ微博
                break;
            case R.id.activity_duanzi_detail_img_sina:
                //TODO 分享到新浪微博
                break;
        }
    }

    //添加emoji表情
    public void addEmoji() {
        List<Integer> list = new LinkedList<Integer>();
        for (int i = 0; i < 20; i++) {

        }

    }

}
